LS.user = {
    /* Register */
    register: function(e) {
        if(!LS.user.validateRegistrationForm()) return;
        LS.user.lockRegistrationForm(true);
        
        LS.alertbox.showError(null);
        
        $.ajax({
            url: LS.getURL("users/reg"),
            type: "POST",
            dataType: "json",
            data: this.getRegistrationFormData(),
            error: function(xhr) {
                LS.alertbox.showError(LS.localized.errorConnection);
                LS.user.lockRegistrationForm(false);
            },
            success: function(response) {
                LS.alertbox.showError(null);
                
                LS.user.lockRegistrationForm(false);
                
                if(response.user.success) {
                    LS.exitModal();
                    LS.showModal('#reg_success_div', this.activateRegister);
                } else {
                    LS.alertbox.showError(response.user.errmsg);
                    if(response.user.error) {
                    	LS.user.showRegError(response.user.error);
                    }
                }
            }
        });
    },
    cancelRegister: function(e) {
        if(!LS.user.validateCancelRegistrationForm()) return;
        LS.lockForm('#cancel_reg_form', true);
        
        LS.alertbox.showError(null);
        
        $.ajax({
            url: LS.getURL("users/unreg"),
            type: "POST",
            dataType: "json",
            data: LS.getFormData('#cancel_reg_form'),
            error: function(xhr) {
                LS.alertbox.showError(LS.localized.errorConnection);
                LS.lockForm('#cancel_reg_form', false);
            },
            success: function(response) {
                LS.alertbox.showError(null);
                LS.lockForm('#cancel_reg_form', false);
                if(response.user.success) {
                    LS.exitModal();
                    LS.showModal('#dereg_success_div', this.activateRegister);
                } else {
                    LS.alertbox.showError(response.user.errmsg);
                }
            }
        });
    },
    /* UI Register Form */
    showRegError: function(errs) {
        if(errs == null) {
            $('#reg_form div.error').hide();
            return;
        }
        var f = null;
        for(var i in errs) {
            f = $('#reg_form input[name="'+i+'"]');
            if(f.length) {
                var ferr = $('.error', f.parent());
                if(!ferr.length) {
                    ferr = $(document.createElement("div")).addClass("error");
                    f.parent().append(ferr);
                }
                ferr.html(errs[i]);
            }
            ferr.show();
        }
    },
    showCancelRegError: function(errs) {
        if(errs == null) {
            $('#cancel_reg_form div.error').hide();
            return;
        }
        var f = null;
        for(var i in errs) {
            f = $('#cancel_reg_form input[name="'+i+'"]');
            if(f.length) {
                var ferr = $('.error', f.parent());
                if(!ferr.length) {
                    ferr = $(document.createElement("div")).addClass("error");
                    f.parent().append(ferr);
                }
                ferr.html(errs[i]);
            }
            ferr.show();
        }
    },
    lockRegistrationForm: function(lock) {
        LS.lockForm('#reg_form', lock);
    },
    validateRegistrationForm: function() {
    	LS.alertbox.showError(null);
        this.showRegError(null);
        
        var errors = {}, error_count = 0;
                
        for(var field in LS.localized.registrationFormFieldErrors) {
            try {
            	LS.checkFormField('#reg_form', field, LS.localized.registrationFormFieldErrors[field]);
            } catch(e) {
                errors[field] = e;
                error_count++;
            }
        }
        
        if($('#reg_form input[name="password"]').val() != $('#reg_form input[name="password_confirm"]').val()) {
            $('#reg_form input[name="password"]').val("");
            $('#reg_form input[name="password_confirm"]').val("");
            errors['password_confirm'] = LS.localized.registrationPasswordConflict;
            error_count++;
        }
        
        if(error_count != 0) {
            this.showRegError(errors);
            return false;
        }
        return true;
    },
    validateCancelRegistrationForm: function() {
    	LS.alertbox.showError(null);
        this.showCancelRegError(null);
        
        var errors = {}, error_count = 0;
        if($('#cancel_reg_form input[name="username"]').val()==""){
            errors['username'] = LS.localized.registrationFormFieldErrors.username;
            error_count++;
        }
        if($('#cancel_reg_form input[name="password"]').val()=="") {
        	errors['password'] = LS.localized.registrationFormFieldErrors.username;
            error_count++;
        }
        
        if(error_count != 0) {
            this.showCancelRegError(errors);
            return false;
        }
        return true;
    },
    getRegistrationFormData: function() {
        return LS.getFormData('#reg_form');
    },
    /* Register */
    launchRegister: function() {
        $('#login_form input[name="username"]').val("");
        $('#reg_form input').each(function() {
            if(this.type == "text" || this.type == "password")
                this.value = "";
        });
        
        LS.showModal('#reg_div', this.activateRegister);
    },
    launchCancelRegister: function() {
        $('#cancel_login_form input[name="username"]').val("");
        $('#cancel_reg_form input').each(function() {
            if(this.type == "text" || this.type == "password")
                this.value = "";
        });
        
        LS.showModal('#cancel_reg_div', this.activateRegister);
    },
    activateRegister: function() {
        $('#reg_form input[name="username"]').focus();
    },
    getJustRegisteredUsername: function() {
        var username = $('#reg_form input[name="username"]').val();
        return (username != "") ? username : null;
    }
};
